package hiatao.puzzle.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.desmond.parallaxheaderviewpager.TestActivity;

import haitao.puzzle.R;
import hiatao.puzzle.config.MyApplication;
import hiatao.puzzle.config.URLS;
import hiatao.puzzle.entity.User;
import hiatao.puzzle.utils.StatusBarCompat;
import hiatao.puzzle.utils.Tools;

public class LoginActivity  extends Activity implements OnClickListener{

    private TextView register_tv;
    private EditText account_et;
    private EditText psw_et;
    private ImageView psw_img;
    private Button login_tv;
    private boolean display= false;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.activity_login); 
		findViews();
		setListeners();
		StatusBarCompat.compat(this, getResources().getColor(R.color.primary));
    }
	protected void findViews() {
        register_tv = (TextView) findViewById(R.id.register_tv);
        account_et = (EditText) findViewById(R.id.account_et);
        psw_et = (EditText) findViewById(R.id.psw_et);
        psw_img = (ImageView) findViewById(R.id.psw_img);
        login_tv = (Button) findViewById(R.id.login_btn);
	}
	protected void setListeners() {
		login_tv.setOnClickListener(this);
		psw_img.setOnClickListener(this);
		findViewById(R.id.top_back).setOnClickListener(this);
		//android:background="@drawable/btn_blue_round"
		
		account_et.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				if (psw_et.getText().toString().length()>0 && account_et.getText().toString().length()>0 && !TextUtils.isEmpty(account_et.getText()) && !TextUtils.isEmpty(psw_et.getText())){
					 login_tv.setBackgroundResource(R.drawable.btn_blue_round);
				 }else{
					 login_tv.setBackgroundResource(R.drawable.btn_gray_round);
				 }
				
			}
		});
		psw_et.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				 if (psw_et.getText().toString().length()>0 && account_et.getText().toString().length()>0 && !TextUtils.isEmpty(account_et.getText()) && !TextUtils.isEmpty(psw_et.getText())){
					 login_tv.setBackgroundResource(R.drawable.btn_blue_round);
				 }else{
					 login_tv.setBackgroundResource(R.drawable.btn_gray_round);
				 }
			}
		});
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.login_btn:
			login();
			break;
		case R.id.psw_img:
			showPsw();
			break;
		case R.id.top_back:
			finish();
			break;

		default:
			break;
		}
	}

	private void showPsw() {
		if(!display){
			psw_et.setTransformationMethod(PasswordTransformationMethod.getInstance());  
			display =true;
		}else{
			psw_et.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
			display =false;
		}
	}
	private void login() {
		if(TextUtils.isEmpty(account_et.getText())||TextUtils.isEmpty(psw_et.getText())){
			Tools.showToast("账号或密码不能为空");
		}else{
			Tools.showWaitDialog(this, "登陆中", false);
			new Handler().postDelayed(new Runnable() {
				@Override
				public void run() {
					Tools.dismissWaitDialog();
					MyApplication.getInstance().isLogin = true;
					MyApplication.getInstance().hasChage = true;
					User u= new User();
					u.setName("轻狂");
					u.setIntro("不达成功誓不休");
					u.setHeadUrl(URLS.BASE_USER+15+".png");
					u.setFocus(false);
					MyApplication.getInstance().user = u;
					Tools.goActivity(LoginActivity.this,TestActivity.class, null, null);
					LoginActivity.this.finish();
				}
			}, 1500);
			
		}
	}
}
